
//读取10K NTC温度
void get_TEM_ntc(){
  float ntc_res_value = (10000*3.3) / (analogRead(ESP_PIN_NTC) / 4095.0 * 3.3) - 10000;
  if(ntc_res_value>=34380){
    for(int i=0;i<20;i++){
      if(ntc_res_value>=ntc_10k_l20_value[i]){
        system_temperature_ntc = i - 20;
        break;
      }
    }
  }else if(ntc_res_value>=2488){
    for(int i=0;i<61;i++){
      if(ntc_res_value>=ntc_10k_u61_value[i]){
        system_temperature_ntc = i;
        break;
      }
    }
  }else{
    for(int i=0;i<60;i++){
      if(ntc_res_value>=ntc_10k_h60_value[i]){
        system_temperature_ntc = i + 61;
        break;
      }
    }
  }
 
}
//初始化传感器
void sensors_init(){
  Serial.println(F("---------------Initialize INA226 0x40---------------"));
  // 配置I2C地址
  ina_40.begin(0x40);
  // 配置INA226
  ina_40.configure(INA226_AVERAGES_4, INA226_BUS_CONV_TIME_588US, INA226_SHUNT_CONV_TIME_588US, INA226_MODE_SHUNT_BUS_CONT);
  // 配置采样电阻及最大电流
  ina_40.calibrate(system_ina_resistor_1, system_ina_Max_excepted_current_1);
  // 检查设置
  checkConfig(ina_40);
  Serial.println(F("----------------------0x40 ok-----------------------"));

  Serial.println(F("---------------Initialize INA226 0x41---------------"));
  // 配置I2C地址
  ina_41.begin(0x41);
  // 配置INA226
  ina_41.configure(INA226_AVERAGES_4, INA226_BUS_CONV_TIME_588US, INA226_SHUNT_CONV_TIME_588US, INA226_MODE_SHUNT_BUS_CONT);
  // 配置采样电阻及最大电流
  ina_41.calibrate(system_ina_resistor_2, system_ina_Max_excepted_current_2);
  // 检查设置
  checkConfig(ina_41);
  Serial.println(F("----------------------0x41 ok----------------------"));

  Serial.println(F("---------------Initialize INA226 0x42---------------"));
  // 配置I2C地址
  ina_42.begin(0x42);
  // 配置INA226
  ina_42.configure(INA226_AVERAGES_4, INA226_BUS_CONV_TIME_588US, INA226_SHUNT_CONV_TIME_588US, INA226_MODE_SHUNT_BUS_CONT);
  // 配置采样电阻及最大电流
  ina_42.calibrate(system_ina_resistor_3, system_ina_Max_excepted_current_3);
  // 检查设置
  checkConfig(ina_42);
  Serial.println(F("----------------------0x42 ok----------------------"));
}
//读取传感器值
void readSensors(){
  //电压、电流、功率读取, 电压上端电阻10k，下端电阻5.1k
  system_ina_voltage_1   = ina_40.readBusVoltage() * 15.1/5.1 + system_ina_voltage_correct_1;
  system_ina_current_1   = fabsf(ina_40.readShuntCurrent() + system_ina_current_correct_1);
  system_ina_power_1     = ina_40.readBusPower() * 15.1/5.1;
  system_ina_voltage_2   = ina_41.readBusVoltage() * 15.1/5.1 + system_ina_voltage_correct_2;
  system_ina_current_2   = fabsf(ina_41.readShuntCurrent() + system_ina_current_correct_1);
  system_ina_power_2     = ina_41.readBusPower() * 15.1/5.1;
  system_ina_voltage_3   = ina_42.readBusVoltage() * 15.1/5.1 + system_ina_voltage_correct_3;
  system_ina_current_3   = fabsf(ina_42.readShuntCurrent() + system_ina_current_correct_1);
  system_ina_power_3     = ina_42.readBusPower() * 15.1/5.1;
  //电量积分
  float temp_power_meter_in   = system_ina_power_1 * (millis() - system_millis_last_meter) / 3600000;
  float temp_power_meter_bat  = system_ina_power_2 * (millis() - system_millis_last_meter) / 3600000;
  float temp_power_meter_out  = system_ina_power_3 * (millis() - system_millis_last_meter) / 3600000;
  system_millis_last_meter = millis();
  //每日电量
  system_power_meter_in   += temp_power_meter_in;
  if(temp_power_meter_in > temp_power_meter_out && temp_power_meter_in > temp_power_meter_bat){
    system_power_meter_bat  += temp_power_meter_bat;
  }
  system_power_meter_out  += temp_power_meter_out;
  //历史电量
  store_in_kwh   += temp_power_meter_in / 1000;
  store_bat_kwh  += temp_power_meter_bat / 1000;
  store_out_kwh  += temp_power_meter_out / 1000;
  //mppt 效率
  if(system_ina_voltage_1>0 && system_ina_current_1>0 && system_ina_power_1>0 && system_ina_power_1>system_ina_power_2){
    system_mppt_efficiency = constrain((system_ina_power_2 / system_ina_power_1)*100,0,100);
  }else{
    system_mppt_efficiency = 0;
  }
  //1s刷新一次
  if(sensors_after_1s()){
    //温度
    get_TEM_ntc();
    //电池电量
    if(system_buck_enable==1){
      system_battery_percent = ((system_ina_voltage_2 - system_bat_minv) / (system_bat_maxv - system_bat_minv)) * 100;
    }else{
      system_battery_percent = ((system_ina_voltage_2 - system_bat_minv) / (system_bat_fullv - system_bat_minv)) * 100;
    }
    system_battery_percent = constrain(system_battery_percent, 0, 100);
  }
}

//1s计时器
bool sensors_after_1s(){
  if(millis()-system_millis_last_sensors>=1000){
    system_millis_last_sensors = millis();
    return true;
  }else{
    return false;
  }
}
// 检查ina226配置
void checkConfig(INA226 &i226)
{
  Serial.print("Mode:                  ");
  switch (i226.getMode())
  {
    case INA226_MODE_POWER_DOWN:      Serial.println(F("Power-Down")); break;
    case INA226_MODE_SHUNT_TRIG:      Serial.println(F("Shunt Voltage, Triggered")); break;
    case INA226_MODE_BUS_TRIG:        Serial.println(F("Bus Voltage, Triggered")); break;
    case INA226_MODE_SHUNT_BUS_TRIG:  Serial.println(F("Shunt and Bus, Triggered")); break;
    case INA226_MODE_ADC_OFF:         Serial.println(F("ADC Off")); break;
    case INA226_MODE_SHUNT_CONT:      Serial.println(F("Shunt Voltage, Continuous")); break;
    case INA226_MODE_BUS_CONT:        Serial.println(F("Bus Voltage, Continuous")); break;
    case INA226_MODE_SHUNT_BUS_CONT:  Serial.println(F("Shunt and Bus, Continuous")); break;
    default: Serial.println("unknown");
  }
  
  Serial.print(F("Samples average:       "));
  switch (i226.getAverages())
  {
    case INA226_AVERAGES_1:           Serial.println(F("1 sample")); break;
    case INA226_AVERAGES_4:           Serial.println(F("4 samples")); break;
    case INA226_AVERAGES_16:          Serial.println(F("16 samples")); break;
    case INA226_AVERAGES_64:          Serial.println(F("64 samples")); break;
    case INA226_AVERAGES_128:         Serial.println(F("128 samples")); break;
    case INA226_AVERAGES_256:         Serial.println(F("256 samples")); break;
    case INA226_AVERAGES_512:         Serial.println(F("512 samples")); break;
    case INA226_AVERAGES_1024:        Serial.println(F("1024 samples")); break;
    default: Serial.println("unknown");
  }

  Serial.print("Bus conversion time:   ");
  switch (i226.getBusConversionTime())
  {
    case INA226_BUS_CONV_TIME_140US:  Serial.println(F("140uS")); break;
    case INA226_BUS_CONV_TIME_204US:  Serial.println(F("204uS")); break;
    case INA226_BUS_CONV_TIME_332US:  Serial.println(F("332uS")); break;
    case INA226_BUS_CONV_TIME_588US:  Serial.println(F("558uS")); break;
    case INA226_BUS_CONV_TIME_1100US: Serial.println(F("1.100ms")); break;
    case INA226_BUS_CONV_TIME_2116US: Serial.println(F("2.116ms")); break;
    case INA226_BUS_CONV_TIME_4156US: Serial.println(F("4.156ms")); break;
    case INA226_BUS_CONV_TIME_8244US: Serial.println(F("8.244ms")); break;
    default: Serial.println("unknown");
  }

  Serial.print("Shunt conversion time: ");
  switch (i226.getShuntConversionTime())
  {
    case INA226_SHUNT_CONV_TIME_140US:  Serial.println(F("140uS")); break;
    case INA226_SHUNT_CONV_TIME_204US:  Serial.println(F("204uS")); break;
    case INA226_SHUNT_CONV_TIME_332US:  Serial.println(F("332uS")); break;
    case INA226_SHUNT_CONV_TIME_588US:  Serial.println(F("558uS")); break;
    case INA226_SHUNT_CONV_TIME_1100US: Serial.println(F("1.100ms")); break;
    case INA226_SHUNT_CONV_TIME_2116US: Serial.println(F("2.116ms")); break;
    case INA226_SHUNT_CONV_TIME_4156US: Serial.println(F("4.156ms")); break;
    case INA226_SHUNT_CONV_TIME_8244US: Serial.println(F("8.244ms")); break;
    default: Serial.println("unknown");
  }
  
  Serial.print(F("Max possible current:  "));
  Serial.print(i226.getMaxPossibleCurrent());
  Serial.println(F(" A"));

  Serial.print(F("Max current:           "));
  Serial.print(i226.getMaxCurrent());
  Serial.println(F(" A"));

  Serial.print(F("Max shunt voltage:     "));
  Serial.print(i226.getMaxShuntVoltage());
  Serial.println(F(" V"));

  Serial.print(F("Max power:             "));
  Serial.print(i226.getMaxPower());
  Serial.println(F(" W"));
}
